package com.beck.code.generator.generatorProcessor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.beck.code.generator.configProperty.CommonGeneratorConfig;
import com.beck.code.generator.configProperty.ContextConfig;
import com.beck.code.generator.configProperty.TableConfig;
import com.beck.code.generator.vo.JavaCodeVO;


/**
 * @author CuiJianbo
 * @since 2025/7/1
 */

public class ServiceGenerator extends CommonGenerator {

    public ServiceGenerator(TableConfig tableConfig, IntrospectedTable introspectedTable, ContextConfig contextConfig) {
        super(tableConfig, introspectedTable, contextConfig, contextConfig.getServiceGeneratorConfig());
    }


    public static ServiceGenerator getInstance(TableConfig tableConfig, IntrospectedTable introspectedTable, ContextConfig contextConfig) {
        return new ServiceGenerator(tableConfig, introspectedTable, contextConfig);
    }

    @Override
    void generateCodeFile() {
        String packageName = contextConfig.getServiceGeneratorConfig().getTargetPackage();
        JavaCodeVO daoVO = new JavaCodeVO();
        BeanUtil.copyProperties(commonGeneratorVO, daoVO);
        daoVO.setPackageName(packageName);
        String data = applyTemplateResources(contextConfig.getServiceGeneratorConfig().getTemplate(), daoVO);
        //输出真实文件
        boolean flag = generateCodeFile(data);

        if (flag) {
            System.out.println(fileName());
        }
    }

    @Override
    String fileName() {
        String fileName = StrUtil.concat(true, StrUtil.upperFirst(entityName), "Service.java");
        return contextConfig.getServiceGeneratorConfig().getTargetPackage().replace(".", "/") + "/" + fileName;
    }
}
